﻿using System;
using System.Text;
using wojilu.Apps.Qycms.Domain;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Routes;
using YBBYunQi;

namespace wojilu.Web.Controller
{
    public class PageInfo
    {
        /// <summary>
        /// 样式
        /// </summary>
        /// <param name="RecordCount"></param>
        /// <param name="PageCount"></param>
        /// <param name="Current"></param>
        /// <param name="Size"></param>
        /// <param name="Lngs"></param>
        /// <returns></returns>
        public String setPagerBar(Int32 RecordCount, Int32 PageCount, Int32 Current, Int32 Size, String Lngs, TypeList t)
        {

            String url = getPath(t, Lngs);

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<div class=\"turnpage\" data-records={0} data-pages={1} data-page-size={2}>", RecordCount, PageCount, Size);
            sb.AppendFormat(lang.get(Lngs + "pageStats"), RecordCount, Size);

            sb.Append("&nbsp;");
            if (Current > 1)
            {
                sb.Append("<a href=\"");
                appendLink(url, sb, Current - 1);
                sb.Append("\" class=\"pagePrev\">&laquo;" + lang.get(Lngs + "prevPage") + "</a>&nbsp;");
            }
            if (Current <= 8)
            {
                loopPage(url, sb, 1, Current);
            }
            else
            {
                loopPage(url, sb, 1, 3);
                sb.Append("...&nbsp;");
                if ((PageCount - Current) < 3)
                {
                    loopPage(url, sb, PageCount - 6, Current);
                }
                else
                {
                    loopPage(url, sb, Current - 3, Current);
                }
            }
            sb.Append("<span class=\"currentPageNo\">");
            sb.Append(Current);
            sb.Append("</span>&nbsp;");
            if ((PageCount - Current) <= 7)
            {
                loopPage(url, sb, Current + 1, PageCount + 1);
            }
            else
            {
                if ((Current + 3) < 7)
                {
                    loopPage(url, sb, Current + 1, 8);
                }
                else
                {
                    loopPage(url, sb, Current + 1, Current + 4);
                }
                sb.Append("...&nbsp;");
                loopPage(url, sb, PageCount - 1, PageCount + 1);
            }
            if (Current < PageCount)
            {
                sb.Append("<a href=\"");
                appendLink(url, sb, Current + 1);
                sb.Append("\" class=\"pageNext\">" + lang.get(Lngs + "nextPage") + "&raquo;</a>&nbsp;");
            }
            sb.Append("</div>");
            return sb.ToString();
        }

        protected string GetDir(String lng)
        {
            return strUtil.Join(sys.Path.Root + YBBCommon.GetConfigStr("file_htmldir"), lng + "/");
        }

        private String getPath(TypeList t, String lng)
        {
            if (ConfigUtil.GetConfigInt("is_html") == 1)
            {
                String htmlPath = String.Empty;
                if (t.dirpath.StrIsNullOrEmpty())
                {
                    htmlPath = strUtil.Join(GetDir(lng), t.dirname) + "/";
                }
                else
                {
                    htmlPath = strUtil.Join(GetDir(lng), t.dirpath) + "/" + t.dirname + "/";
                }
                _path = htmlPath;
            }
            if (_path != null) return _path;

            String routePath = Route.getRoutePath();
            String path = (routePath == null ? CurrentRequest.getRawUrl() : routePath);
            _path = path;
            return path;
        }

        private String _path;

        private void loopPage(String url, StringBuilder sb, int startNo, int endNo)
        {
            for (int i = startNo; i < endNo; i++)
            {
                sb.Append("<a href=\"");
                appendLink(url, sb, i);
                sb.Append("\" class=\"pageNo\">");
                sb.Append(i);
                sb.Append("</a>&nbsp;");
            }
        }

        private void appendLink(String url, StringBuilder sb, int pageNo)
        {
            sb.Append(AppendNo(url, pageNo));
        }

        /// <summary>
        /// 在已有网址url后加上页码 Post/List.aspx=>Post/List/p6.aspx
        /// </summary>
        /// <param name="srcUrl">原始网址</param>
        /// <param name="pageNumber">页码</param>
        /// <returns></returns>
        public String AppendNo(String srcUrl, int pageNumber)
        {

            if (strUtil.IsNullOrEmpty(srcUrl)) return srcUrl;

            String url = srcUrl;

            // 查询字符串
            int qIndex = url.IndexOf("?");
            String query = "";
            if (qIndex > 0)
            {
                url = srcUrl.Substring(0, qIndex);
                query = srcUrl.Substring(qIndex, (srcUrl.Length - qIndex));
            }

            String ext = getExt(url);

            // 有扩展名
            if (ext.Length > 0)
            {
                url = strUtil.TrimEnd(url, ext);
            }

            String originalPage = getPageNumberLabel(url);
            if (originalPage.Length > 0) url = strUtil.TrimEnd(url, originalPage);

            if (pageNumber <= 1)
            {
                if (ConfigUtil.GetConfigInt("is_html") == 1)
                {
                    return strUtil.Join(url, YBBCommon.GetConfigStr("entrance_file") + ext) + query;
                }
                return url + ext + query;
            }
            else
            {
                if (ConfigUtil.GetConfigInt("is_html") == 1)
                {
                    return strUtil.Join(url , MvcConfig.Instance.UrlSeparator + "p")  + pageNumber + ext + query;
                }
                return url + MvcConfig.Instance.UrlSeparator + "p" + pageNumber + ext + query;
            }
        }

        /// <summary>
        /// 网址后缀扩展名
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private String getExt(String url)
        {
            if (ConfigUtil.GetConfigInt("is_html") == 1)
            {
                return "." + YBBCommon.GetConfigStr("file_fileex");
            }
            return MvcConfig.Instance.UrlExt;
        }

        private String getPageNumberLabel(String url)
        {

            if (strUtil.IsNullOrEmpty(url)) return "";

            String[] arr = url.Split(RouteTool.Separator);
            if (arr.Length < 2) return "";

            String end = arr[arr.Length - 1];

            if (end.StartsWith("p") && cvt.IsInt(end.Substring(1)))
            {
                return MvcConfig.Instance.UrlSeparator + end;
            }

            return "";
        }

    }
}
